package com.example.habits;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Class responsible for creating databases.
 * 
 * @author Oskar
 */
public class DatabaseHandler extends SQLiteOpenHelper {

	
	private static final int DATABASE_VERSION = 1;

	public static final String TABLE_UNLOCK = "unlockTable";
	public static final String COLUMN_ID = "rowid";
	public static final String COLUMN_TIMESTAMP = "timestamp";
	public static final String COLUMN_DURATION = "duration";
	public static final String COLUMN_WEEK = "week";
	public static final String COLUMN_UNIXTIMESTAMP = "unixtimestamp";

	/** Constant to create database */
	private static final String DATABASE_CREATE =
			"CREATE TABLE " + TABLE_UNLOCK + " (" +
					COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,  " +
					COLUMN_TIMESTAMP + " TEXT, " +
					COLUMN_UNIXTIMESTAMP + " LONG, " +
					COLUMN_WEEK + " INT, " +
					COLUMN_DURATION + " TEXT);";

	/** Constant if GPS-coordinates will be used in the future */
	public static final String COLUMN_COORDINATES = "coordinates";

	public DatabaseHandler(Context context) {
		super(context, TABLE_UNLOCK, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		System.out.println("onCreate: " + db.toString());
		db.execSQL(DATABASE_CREATE);
	}

	/**
	 * The onUpgrade() method is called when the handler is invoked with 
	 * a greater database version number from the one previously used.
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_UNLOCK);
		onCreate(db);
	}
}

